/**
 * @author banglong
 * @date 2016/9/2.
 */
define(['Layer', 'text!../html/components/bill/batchInfoTemp.html!strip', 'avalon', 'tools'],
  function (layer, htmlTemp, avalon, tools) {

    var batchInfoVm;
    var getVmOption = function () {
      return {
        $id: 'batchInfo',
        ApplyNum: 0,
        ActualSendNum: 0,
        RefPrice: 0,
        CardNumberList: '',
        batchList: []
      }
    };

    var layeroObj;
    var good;
    var api;

    var init = function (param) {

      layeroObj = tools.createNewOpenWindow(['600px', '493px'], '明细批次', htmlTemp);
      batchInfoVm = tools.initAvalonVm(avalon, getVmOption(), '.batch-info-dialog');

      good = param.good;
      batchInfoVm.ApplyNum = good.ApplyNum;
      batchInfoVm.ActualSendNum = good.ActualSendNum || '';
      batchInfoVm.RefPrice = good.RefPrice;
      batchInfoVm.CardNumberList = good.CardNumberList || '';

      api = param.api;
      var index = param.index;
      batchInfoVm.batchList.splice(0, batchInfoVm.batchList.length);
      var layerIndex = layer.load();

      api.getBill({
        className: 'MoveBillQueryManager',
        method: 'GetBill',
        billSN: good.BillSN
      }).then(function (jsonResult) {

        if (jsonResult.issuccess !== 0 && jsonResult.result !== 'null') {
          return Promise.reject('查询信息失败！');
        }

        var result = JSON.parse(jsonResult.result);
        var DetailBatches = result.Details[index].DetailBatches;
        batchInfoVm.batchList.pushArray(DetailBatches);
        layer.close(layerIndex);

      })['catch'](function (ex) {
        tools.handleError(ex);
      });
    };

    return {
      init: init
    };
  });